package org.kl.bpm.service.conf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kl.bf.utils.CRUDUtils;
import org.kl.bpm.entity.conf.BpmConfListener;
import org.kl.bpm.entity.conf.BpmConfNode;
import org.kl.bpm.repository.conf.BpmConfListenerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.utils.Collections3;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmConfListenerService {

	private BpmConfListenerDao bpmConfListenerDao;

	public BpmConfListener getBpmConfListener(String id) {
		return bpmConfListenerDao.findOne(id);
	}

	public BpmConfListener findByVTB(String value, Integer type, BpmConfNode bpmConfNode) {
		return bpmConfListenerDao.findByVTB(value, type, bpmConfNode);
	}

	public BpmConfListener findByVTB(String value, Integer type, String bpmConfNodeId) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("EQ_value", value);
		searchParams.put("EQ_type", type);
		searchParams.put("EQ_status", 1);
		searchParams.put("EQ_bpmConfNode.id", bpmConfNodeId);
		Specification<BpmConfListener> spec = buildSpecification(searchParams);
		List<BpmConfListener> bclList = bpmConfListenerDao.findAll(spec);
		if (Collections3.isNotEmpty(bclList)) {
			return bclList.get(0);
		} else {
			return null;
		}
	}

	public void save(BpmConfListener entity) {
		bpmConfListenerDao.save(entity);
	}

	public void delete(String id) {
		bpmConfListenerDao.delete(id);
	}

	public List<BpmConfListener> getAllBpmConfListener() {
		return bpmConfListenerDao.findAll();
	}
	//带条件的查询
	public List<BpmConfListener> searchAll(Map<String, Object> searchParams, String sortType,String orderType) {
		List<BpmConfListener> list = bpmConfListenerDao.searchAll(searchParams, sortType, orderType, BpmConfListener.class);
		return list;
	}

	public Page<BpmConfListener> getUserBpmConfListener(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmConfListener> spec = buildSpecification(searchParams);

		return bpmConfListenerDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmConfListener> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmConfListener> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmConfListener.class);
		return spec;
	}

	@Autowired
	public void setBpmConfListenerDao(BpmConfListenerDao bpmConfListenerDao) {
		this.bpmConfListenerDao = bpmConfListenerDao;
	}

}
